const app = getApp();
import services from '../../services/clockin';
import moment from 'moment';
import common from '../../utils/common';

Page({
  data: {
    stationList: [],
    formData: {
      mode: 0, // 0工地补卡，1外勤补卡
      clockType: 0, // 打卡类型 0: 上班打卡 1: 下班打卡
      shiftType: 0, // 班次：0白班 1夜班
      pointId: '',
      reissueTime: '',
      reason: ''
    },
    modeOptions: [{
      title: "工地补卡",
    }, {
      title: "外勤补卡"
    }],
    clockTypeOptions: [{
        value: 0,
        label: '上班打卡'
      },
      {
        value: 1,
        label: '下班打卡'
      },
    ],
    shiftTypeOptions: [{
        value: 0,
        label: '白班'
      },
      {
        value: 1,
        label: '夜班'
      },
    ],
    isLoading: false,
    isReady: false,
    defaultSiteId: '',
    defaultMode: '',
    defaultClockType: '',
    defaultShiftType: '',
    unionid: '',
  },
  async onLoad(query) {
    const {
      unionid
    } = app.globalData.userInfo || {};
    this.setData({
      unionid
    })
    const {
      stationId,
      mode,
      clockType,
      shiftType,
    } = query || {}

    this.setData({
      defaultSiteId: stationId || '',
      defaultMode: mode || '0',
      defaultClockType: clockType || '0',
      defaultShiftType: shiftType || '0',
    })
    await this.queryGetStation()
    this.setData({
      isReady: true
    })
  },
  async onShow() {
    if (this.data.isReady) {
      await this.queryGetStation()
    }
  },
  async queryGetStation() {
    try {
      const res = await services.getStation(this.data.unionid);
      if (res && res.data && res.data.length) {
        const stationList = res.data.map(item => {
          const {
            name,
            id
          } = item || {}
          return {
            label: name,
            value: id
          }
        })
        this.setData({
          stationList,
          formData: {
            ...this.data.formData,
            pointId: Number(this.data.defaultSiteId) || '',
            mode: Number(this.data.defaultMode) || 0,
            clockType: Number(this.data.defaultClockType) || 0,
            shiftType: Number(this.data.defaultShiftType) || 0
          }
        })
        console.log('stationList', this.data);
      }
    } catch (error) {
      console.log("获取补卡列表失败", error);
    }
  },
  handleChangeMode(current) {
    this.setData({
      formData: {
        ...this.data.formData,
        mode: current || 0
      }
    })
  },
  handleChangeClockType(value) {
    this.setData({
      formData: {
        ...this.data.formData,
        clockType: value || 0
      }
    })
  },
  handleChangeShiftType(value) {
    this.setData({
      formData: {
        ...this.data.formData,
        shiftType: value || 0
      }
    })
  },
  handleSetPointId(value) {
    this.setData({
      formData: {
        ...this.data.formData,
        pointId: value
      }
    });
  },
  handleSetReissueTime(value) {
    this.setData({
      formData: {
        ...this.data.formData,
        reissueTime: value
      }
    });
  },
  handleSetReason(value) {
    this.setData({
      formData: {
        ...this.data.formData,
        reason: value
      }
    });
  },
  async submit() {
    const {
      mode,
      clockType,
      shiftType,
      pointId,
      reissueTime,
      reason
    } = this.data.formData
    if (mode === 0 && !pointId) {
      dd.showToast({
        type: 'info',
        content: '请选择补卡工地',
        duration: 1500,
      });
      return
    }
    if (!reissueTime) {
      dd.showToast({
        type: 'info',
        content: '请选择补卡时间',
        duration: 1500,
      });
      return
    }
    if (!reason) {
      dd.showToast({
        type: 'info',
        content: '请输入补卡理由',
        duration: 1500,
      });
      return
    }
    const params = {
      unionid: this.data.unionid || '',
      clock_datetime: moment(reissueTime).format('YYYY-MM-DD HH:mm:ss'),
      clock_type: clockType,
      reason: reason
    }
    if (mode === 0) {
      // 工地补卡
      params.station_id = pointId
      params.shift_type = shiftType
      await this.queryPostReissue(params)
    } else {
      // 外勤补卡
      await this.queryPostOutReissue(params)
    }
    console.log(params)
  },
  async queryPostReissue(params) {
    if (this.data.isLoading) {
      return
    }
    this.setData({
      isLoading: true
    })
    try {
      this.setData({
        isLoading: false
      })
      await services.postReissue(params);
      dd.showToast({
        type: 'success',
        content: '提交成功',
        duration: 2000,
        success: () => {
          dd.navigateBack({
            delta: 1
          });
        },
      });

    } catch (error) {
      console.log(error)
      common.showErrorToast(error)
    }
  },
  async queryPostOutReissue(params) {
    if (this.data.isLoading) {
      return
    }
    this.setData({
      isLoading: true
    })
    try {
      this.setData({
        isLoading: false
      })
      await services.postOutReissue(params);
      dd.showToast({
        type: 'success',
        content: '提交成功',
        duration: 2000,
        success: () => {
          dd.navigateBack({
            delta: 1
          });
        },
      });
    } catch (error) {
      console.log(error)
      common.showErrorToast(error)
    }
  }
});